home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
pc
/
Code
/
Chapter Code.exe
/
Chapter08
/
TranslateXYPanel.java
< prev
next >
Wrap
Text File
|
2000-06-27
|
2KB
|
91 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class TranslateXYPanel extends Shout3DPanel implements DeviceObserver{
Transform boxTrans;
float[] worldPos = new float[3];
int pixelStartX;
int pixelStartY;
int pixelEndX;
int pixelEndY;
public TranslateXYPanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
boxTrans = (Transform) getNodeByName("mytrans");
//get reference to position array
//from the transform
//and store in worldPos
worldPos = boxTrans.translation.getValue();
//print out values in worldPos
//to Java Console
System.out.println("Box's x position is " + worldPos[0]);
System.out.println("Box's y position is " + worldPos[1]);
System.out.println("Box's z position is " + worldPos[2]);
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.DRAG:
pixelEndX = mi.x;
pixelEndY = mi.y;
//get pixel distances dragged
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
//convert pixel distance to meters
// at 50 pixels/1 meter
float deltaX = dragDistanceX/50f;
float deltaY = -(dragDistanceY/50f);
//add deltas to current X and Y
//to get new position
worldPos[0] = worldPos[0] + deltaX;
worldPos[1] = worldPos[1] + deltaY;
//put the updated position array
//in the transform node.
boxTrans.translation.setValue(worldPos);
//reset the starting pixel for next drag
pixelStartX = pixelEndX;
pixelStartY = pixelEndY;
return true;
}//end of switch
return false;
}
} //end of class